﻿Imports CCMSClass
Imports System.Data.SqlClient

Public Class frmLogin

    Dim con As New SqlConnection(strcon)

    Private Sub frmLogin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        reset()
    End Sub

    Private Sub frmLogin_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        btnExit_Click(Nothing, Nothing)
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Application.Exit()
    End Sub

    Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click
        comparedUser()
    End Sub

    Protected Sub reset()
        txtUser.Clear()
        txtPass.Clear()
        txtUser.Focus()
    End Sub

    Protected Sub comparedUser()
        Dim oUser As New cUser
        Dim dt As New DataTable
        Dim Role As String = ""
        Dim LawyerID As Long = 0
        Dim isCorrect As Boolean = False
        dt = oUser.select_User()
        Dim i As New Integer
        For i = 0 To dt.Rows.Count - 1
            If dt.Rows(i)("UserID") = txtUser.Text And dt.Rows(i)("Password") = txtPass.Text Then
                Role = dt.Rows(i)("Role")
                If dt.Rows(i)("LawyerID") IsNot DBNull.Value Then
                    LawyerID = dt.Rows(i)("LawyerID")
                End If
                isCorrect = True
                Exit For
            End If
        Next

        If isCorrect Then
            frmMain._Role = Role
            frmMain._LawyerID = LawyerID
            frmMain._User = txtUser.Text
            frmMain.Show()
            reset()
            Hide()
        Else
            MessageBox.Show("អ្នកប្រើប្រាស់និងលេខសម្ងាត់ មិនត្រឹមត្រូវ!", "ប្រព័ន្ធសុវត្ថិភាព", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtPass.Clear()
            txtPass.Focus()
        End If
    End Sub

    Private Sub txtUser_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtUser.KeyDown, txtPass.KeyDown
        If e.KeyCode = Keys.Enter Then
            btnLogin_Click(Nothing, Nothing)
        End If
    End Sub
End Class
